home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / '92_HACK / IR_MAN_ / CD_ROM.C < prev    next >
C/C++ Source or Header  |  1992-06-07  |  7KB  |  283 lines

  1. #include "CD ROM.h"
  2.  
  3.  
  4.  
  5. short Decimal2BCD(short n) {
  6.     return ((n / 10) << 4) + (n % 10); 
  7. }
  8.  
  9. short BCD2Decimal(short n) {
  10.     return ((n >> 4) * 10) + (n & 0x0f);
  11. }
  12.  
  13. /************************************************************************
  14.  *
  15.  *  Function:        AStop
  16.  *
  17.  *  Purpose:        stop playing an audio track
  18.  *
  19.  *  Returns:        OSErr.  Probably either
  20.  *                        noErr        everything's hunky-dory!
  21.  *                        paramErr    you messed up the call somehow.
  22.  *
  23.  *  Side Effects:    stops play.
  24.  *
  25.  *  Description:    The track that you pass in is the LAST track that you
  26.  *                    want to have play.  This means that if you wanted to
  27.  *                    play only one track, you'd pass the same value to
  28.  *                    this routine and APlay() [q.v.]
  29.  *
  30.  *                    Note that this routine isn't called in this sample,
  31.  *                    but it's included for your enjoyment.
  32.  *
  33.  ************************************************************************/
  34. OSErr
  35. AStop(track, refNum)
  36. short    track;
  37. short    refNum;
  38. {
  39.     struct {
  40.         short    addrFormat;
  41.         long    address;
  42.     }    csParam;
  43.     
  44.     csParam.addrFormat = TRACKADDR;
  45.     csParam.address = track;
  46.     return (Control(refNum, ASTOP, &csParam));
  47. }
  48.  
  49. OSErr
  50. APause(short state, short refNum) { 
  51.     struct {
  52.         long    pauseState;  /* 1 pause, 0 play */
  53.     }    csParam;
  54.     
  55.     csParam.pauseState = state;
  56.     return (Control(refNum, APAUSE, &csParam));
  57. }
  58.  
  59. OSErr
  60. CDEject(short ioRefNum) {
  61.     OSErr            osErr;
  62.     Str255            ioName;
  63.     HVolumeParam    *pb;
  64.     
  65.     pb = (HVolumeParam *) NewPtrClear(sizeof (*pb));
  66.     osErr = MemError();
  67.     if (0 != pb && noErr == osErr) {
  68.         (*pb).ioNamePtr    = ioName;
  69.         (*pb).ioVolIndex = 0;
  70.         do {
  71.             ++(*pb).ioVolIndex;
  72.             osErr = PBHGetVInfo((HParmBlkPtr)pb, false);
  73.             if (noErr != osErr) {
  74.                 DisposPtr((Ptr) pb);
  75.                 return osErr;
  76.             }
  77.         } while ((*pb).ioVRefNum != ioRefNum);
  78.         osErr = PBEject((ParmBlkPtr)pb);
  79.         if (noErr == osErr)
  80.              osErr = PBUnmountVol((ParmBlkPtr)pb);
  81.         DisposPtr((Ptr) pb);
  82.     }
  83.     return osErr;
  84. }
  85.  
  86. /************************************************************************
  87.  *
  88.  *  Function:        APlay
  89.  *
  90.  *  Purpose:        start playing an audio track
  91.  *
  92.  *  Returns:        OSErr.  Probably either
  93.  *                        noErr        everything's hunky-dory!
  94.  *                        paramErr    you messed up the call somehow.
  95.  *
  96.  *  Side Effects:    starts play.  By default, this will play until the
  97.  *                    end of the disc.
  98.  *
  99.  *  Description:    The track that you pass in is the first track that you
  100.  *                    want to have play.
  101.  *
  102.  ************************************************************************/
  103. OSErr
  104. APlay(track, refNum)
  105. short    track;
  106. short    refNum;
  107. {
  108.     struct {
  109.         short    addrFormat;
  110.         long    address;
  111.         short    stopAddress;
  112.         short    playMode;
  113.     }    csParam;
  114.     
  115.     csParam.addrFormat = TRACKADDR;
  116.     csParam.address = track;    /* must be in BCD */
  117.     csParam.stopAddress = 0;
  118.     csParam.playMode = STEREO;
  119.     return (Control(refNum, APLAY, &csParam));
  120. }
  121.  
  122.  
  123. /************************************************************************
  124.  *
  125.  *  Function:        Play
  126.  *
  127.  *  Purpose:        play a track, given it's "filename"
  128.  *
  129.  *  Returns:        OSErr
  130.  *                        whatever is returned by APlay()
  131.  *
  132.  *  Side Effects:    starts play of the audio track.
  133.  *
  134.  *  Description:    The two parameters passed in are the file name (which
  135.  *                    will always be of the form "Track XX", XX varying from
  136.  *                    " 1" to "99") and the driver reference number. Extract
  137.  *                    the track number and use it to call APlay() [q.v.]
  138.  *
  139.  ************************************************************************/
  140. OSErr
  141. Play(track, driveNumber)
  142. short         track;
  143. short        driveNumber;
  144. {
  145.     short    trackBCD;
  146.     OSErr    result;
  147.     short    DriverNum;
  148.     
  149.     trackBCD = Decimal2BCD(track);
  150.     DriverNum = GetDrvRefNum(driveNumber);
  151.     return( APlay(trackBCD, DriverNum) );
  152. }
  153.  
  154.  
  155. /************************************************************************
  156.  *
  157.  *  Function:        GetDrvRefNum
  158.  *
  159.  *  Purpose:        Get the driver reference number
  160.  *
  161.  *  Returns:        short.  The driver reference number
  162.  *
  163.  *  Side Effects:    none.
  164.  *
  165.  *  Description:    PBHGetVInfo() will retrieve the driver reference
  166.  *                    number, given the vRefNum associated with a file.
  167.  *                    We acquired the vRefNum as something passed into
  168.  *                    the program, either by SFGetFile() or by 
  169.  *                    GetAppFile().
  170.  *
  171.  *                    We'll use the driver reference number in all our
  172.  *                    control calls to the driver.
  173.  *
  174.  ************************************************************************/
  175. short
  176. GetDrvRefNum(vRefNum)
  177. short    vRefNum;
  178. {
  179.     HParamBlockRec    io;
  180.     
  181.     io.volumeParam.ioCompletion = NULL;
  182.     io.volumeParam.ioNamePtr = NULL;
  183.     io.volumeParam.ioVRefNum = vRefNum;
  184.     io.volumeParam.ioVolIndex = 0;
  185.     PBHGetVInfo(&io, false);
  186.     return io.volumeParam.ioVDRefNum;
  187. }
  188.  
  189.  
  190. OSErr
  191. AScan(short state, short refNum) { 
  192.     AudioStateStruct    aState;
  193.     OSErr                err;
  194.     auto    AScanRec        *pb;    
  195.     
  196.     err = AStatus(refNum, &aState);
  197.     if (err == noErr) {
  198.         pb = (AScanRec *) NewPtrClear(sizeof (*pb));
  199.         err = MemError();
  200.         if (0 != pb && noErr == err) {
  201.             (*pb).ioRefNum             = refNum;
  202.             (*pb).csCode            = ASCAN;
  203.             (*pb).csParam.commandType        = MSFADDR;
  204.             (*pb).csParam.addr[1]    = aState.absMinBCD;
  205.             (*pb).csParam.addr[2]    = aState.absSecBCD;
  206.             (*pb).csParam.addr[3]    = aState.absBlkBCD;
  207.             (*pb).csParam.direction    = state;
  208.             err = PBControl((ParmBlkPtr)pb, false);
  209.             DisposPtr((Ptr) pb);
  210.         }
  211.     }
  212.     return err;
  213. }
  214.  
  215. pascal    OSErr 
  216. AStatus(short ioRefNum, AudioStateStruct *aState) {
  217.  
  218.     auto    OSErr            osErr;
  219.     auto    AStatusRec        *pb;
  220.  
  221.     pb = (AStatusRec *) NewPtrClear(sizeof (*pb));
  222.     osErr = MemError();
  223.     if (0 != pb && noErr == osErr) {
  224.         (*pb).ioRefNum                = ioRefNum;
  225.         (*pb).csCode                = ASTATUS;
  226.         osErr = PBControl((ParmBlkPtr)pb, false);
  227.         if (noErr == osErr) {
  228.             aState->aStatusFlags    = (*pb).csParam.audioStatus;
  229.             aState->playMode        = (*pb).csParam.playMode;
  230.             aState->controlField        = (*pb).csParam.cntlField;
  231.             aState->absMinBCD        = (*pb).csParam.minutes;
  232.             aState->absSecBCD        = (*pb).csParam.seconds;
  233.             aState->absBlkBCD        = (*pb).csParam.frames;
  234.         }
  235.         DisposPtr((Ptr) pb);
  236.     }
  237.     return osErr;
  238. }
  239.  
  240.  
  241. OSErr
  242. ATrackSkip(short state, short refNum) { 
  243.     OSErr        err;
  244.     short        track;
  245.     short        extraTrack;
  246.     
  247.     err = GetCurrTrack(refNum, &track);
  248.     if (err == noErr) {
  249.         if (state) {
  250.             extraTrack = BCD2Decimal(track);
  251.             extraTrack++;
  252.             track = Decimal2BCD(extraTrack);
  253.         }
  254.         else {
  255.             extraTrack = BCD2Decimal(track);
  256.             extraTrack--;
  257.             track = Decimal2BCD(extraTrack);
  258.         }
  259.         APlay(track, refNum);
  260.     }
  261.     return err;
  262. }
  263.  
  264. pascal    OSErr
  265. GetCurrTrack(short ioRefNum, short *Track) {
  266.  
  267.     auto    OSErr            osErr;
  268.     auto    ReadQRec        *pb;
  269.     
  270.     pb = (ReadQRec *) NewPtrClear(sizeof (*pb));
  271.     osErr = MemError();
  272.     if (0 != pb && noErr == osErr) {
  273.         (*pb).ioRefNum                    = ioRefNum;
  274.         (*pb).csCode                    = csReadQ;
  275.         osErr = PBControl((ParmBlkPtr)pb, false);
  276.         if (noErr == osErr) {
  277.             *Track             = (*pb).csParam.track;
  278.         }
  279.         DisposPtr((Ptr) pb);
  280.     }
  281.     return osErr;
  282. }
  283.